package com.central.rearService.service.serviceImp

import cn.hutool.core.util.ObjectUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.central.common.entity.Like
import com.central.common.enum.LikeTypeEnum
import com.central.common.model.CommonResult
import com.central.common.model.RealDataResult
import com.central.common.model.SysUser
import com.central.common.model.success
import com.central.common.service.impl.SuperServiceImpl
import com.central.rearService.mapper.LikeMapper
import com.central.rearService.service.LikeService
import org.springframework.stereotype.Service

/**
 * @author  fly
 * @date  2021/5/26 14:53
 * @version 1.0
 */
@Service
open class LikeServiceImp : SuperServiceImpl<LikeMapper, Like>(), LikeService {


    override fun resolveLike(likeId: Long, sysUser: SysUser, likeType: Int): CommonResult<Boolean> = success(
        getOne(KtQueryWrapper(Like::class.java).eq(Like::likeId, likeId)
            .eq(Like::createBy, sysUser.id))?.run { removeById(id) }
            ?: save(Like(likeId = likeId, type = likeType).apply { createBy = sysUser.id })
    , "操作成功")

    override fun getLikeNum(likeId: Long, likeTypeEnum: LikeTypeEnum): RealDataResult = RealDataResult()
        .apply { likeNum = count(KtQueryWrapper(Like::class.java)
            .eq(Like::likeId, likeId).eq(Like::type, likeTypeEnum.code)) }

    override fun booleanLike(likeId: Long, userId: Long, likeTypeEnum: LikeTypeEnum): RealDataResult = RealDataResult()
        .apply { booleanLike = ObjectUtil.isNotEmpty(getOne(KtQueryWrapper(Like::class.java)
            .eq(Like::likeId, likeId).eq(Like::createBy, userId)
            .eq(Like::type, likeTypeEnum.code))) }
}
